/**
 * 
 */
package org.iridium.allcomments.persistance;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.iridium.allcomments.common.Constants;

/**
 * @author iridium
 * 
 */
public class CommentsDAOImpl extends AbstractSystemDAO implements CommentsDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.CommentsDAO#addItem(java.lang.String,
	 * java.lang.String, java.util.Date, java.lang.String, java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void addItem(long userId, String commentId, String preCommentId,
			String url, String content, String ip) {
		this.buildConnection();
		String sql = "insert into comments(user_id, comment_id, pre_comment_id, url, content, ip) values(?, ?, ?, ?, ?, ?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.setString(2, commentId);
			ps.setString(3, preCommentId);
			ps.setString(4, url);
			ps.setString(5, content);
			ps.setString(6, ip);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		this.closeConnection();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.CommentsDAO#delItem(java.lang.String)
	 */
	@Override
	public void delItem(String commentId) {
		this.buildConnection();
		String sql = "delete from comments where comment_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, commentId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.CommentsDAO#delItemFromOne(java.lang
	 * .String)
	 */
	@Override
	public void delItemFromOne(long userId) {
		this.buildConnection();
		String sql = "delete from comments where user_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.CommentsDAO#updateContent(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public void updateContent(String commentId, String content) {
		this.buildConnection();
		String sql = "update comments set content = ? where comment_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, content);
			ps.setString(2, commentId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.iridium.allcomments.persistance.CommentsDAO#updateTags(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public void updateTags(String commentId, String tags) {
		this.buildConnection();
		String sql = "update comments set tags = ? where comment_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, tags);
			ps.setString(2, commentId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.iridium.allcomments.persistance.CommentsDAO#updateTags(java.lang
	 * .String, java.util.HashSet)
	 */
	@Override
	public void updateTags(String commentId, HashSet<String> tags) {
		this.buildConnection();
		String sql = "update comments set tags = ? where comment_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, this.getTagsString(tags));
			ps.setString(2, commentId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

	private String getTagsString(HashSet<String> tags) {
		StringBuffer sb = new StringBuffer();
		for (String tag : tags) {
			sb.append(tag);
			sb.append(Constants.TAG_SEPARATOR);
		}
		return sb.toString();
	}

	@Override
	public void delTagsFromOne(long userId) {
		this.buildConnection();
		String sql = "update comments set tags = '' where user_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

	@Override
	public List<String[]> queryTagsFromOne(long userId) {
		List<String[]> list = new ArrayList<String[]>();
		this.buildConnection();
		ResultSet result = null;
		String sql = "select comment_id, tags from comments where user_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			result = ps.executeQuery();
			while (result.next()) {
				list.add(new String[] { result.getString(1),
						result.getString(2) });
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
		return list;
	}
}
